package me.hacklh.utils;

import java.io.*;

public class FileUtils {

    /**
     * 返回文件的内容
     *
     * @param fileName
     * @return
     */
    public static String getFileContent(String fileName) {
        File file = new File(fileName);
        String content = "";
        Reader reader = null;
        try {

            reader = new InputStreamReader(new FileInputStream(file));
            int tmp_c;
            while ((tmp_c = reader.read()) != -1) {
                content += (char) tmp_c;
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }

    /**
     * 使用rsa算法加密文件，加密内容使用base64编码保存到 en_file，同时会返回加密内容
     *
     * @param public_key 加密文件使用的公钥
     * @param raw_file   待加密的文件路径
     * @param en_file    加密后内容存放的文件路径
     * @return
     * @throws Exception
     */
    public static String encryptFile(String public_key, String raw_file, String en_file) throws Exception {
        String enData = "";
        String content = getFileContent(raw_file);
        byte[] data = content.getBytes();
        //使用rsa加密文件
        byte[] encodedData = RSAUtils.encryptByPublicKey(data, public_key);
        //使用base64将加密后的内容编码
        enData = Base64Utils.encode(encodedData);
//        System.out.println("加密后内容: \r\n" + enData);

        //保存到文件
        FileWriter fw = new FileWriter(en_file);
        fw.write(enData, 0, enData.length());
        fw.flush();
        fw.close();
        return enData;
    }

    /**
     * 使用rsa算法解密文件，返回解密后的内容
     *
     * @param private_key 解密用的私钥
     * @param en_file     存放加密内容的文件路径
     * @return
     * @throws Exception
     */
    public static String decodeFile(String private_key, String en_file) throws Exception {
        String en_data = getFileContent(en_file);
        //加密的内容以base64保存，所以首先用base64解码，在用rsa解密
        byte[] decodedData = RSAUtils.decryptByPrivateKey(Base64Utils.decode(en_data), private_key);
        String target = new String(decodedData);
//        System.out.println("解密后内容: \r\n" + target);
        return target;
    }


    public static void main(String[] args) throws Exception {
//        encryptFile("C:\\Users\\Administrator\\IdeaProjects\\obfuscator\\src\\xsk.vm", "f:\\encode.txt");

        String private_key = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKAiKDxq2Ly6/gBjz+c1VIjH4UQS2z2doOCMqSncwmMcMN689x7e5A7Bo8c4MOyiUOgy4fBzqZy6rScJVjbkj9kTQaw04PKQ/iCSmgIKmEIXsE2EJCBrr197VmW+yoiEB9+tdxLo1OPl20EPFai3k1a9oXi2vmwDtejsrlfomBHbAgMBAAECgYEAlRY4D8gKcexGf2ZuMfzc71voxtAnfn47QdSzyl3WPSAJ/9Y+3Wb3PSXbracLJcUZPpXg9VU6HMlQXH+83O5/XUU3IuRJxoROxMfZvRBYu4VaKbRrYA7np4SDwHzxoto/GZY9sm641rRZNoh3T/bKeUQ+zoRw6zVaSUbZVcTGb0ECQQDt+JGwK1uHAfZc6L5qHFtTBth2hmilvGfnTbyPa7bMPVUf33MLVd/jDkXlfvF0TLTS0X8oJihYKtnDSws5+S6lAkEArEPvI1nyJHIS9HtfLMt3vktYU9ijn+vTCsXhEVq5orRFhWfKpFrX6xqsAlw+b9cHWx7XRgeZzheRY5QeAEzWfwJAPQGJT9F9sFm3b9AW6JsGan2JxrDkaQCo3mpWWaoyfZ+HkBk3pANK1ymUj6YAfmEfxiAA6ikGFLw/gUpewWj5JQJAS1zA/sc3zsf1EO30OkWdUivmLXBGTQ0/W6QsDYqSUeLp0y4QEg0IDvuxKqsuSYFZ33xDk4h5uhbHbTf8VAdJgwJBAKfjRZ6L+r8Ykq43amYA3DiQbd7qW9BKJ0BpStxmSFlGaUP7huoy4OJmENcHWtxS0Q+jXc/Dz22Vyocwt9hjzh8=";
        String public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCgIig8ati8uv4AY8/nNVSIx+FEEts9naDgjKkp3MJjHDDevPce3uQOwaPHODDsolDoMuHwc6mcuq0nCVY25I/ZE0GsNODykP4gkpoCCphCF7BNhCQga69fe1ZlvsqIhAffrXcS6NTj5dtBDxWot5NWvaF4tr5sA7Xo7K5X6JgR2wIDAQAB";

        decodeFile(private_key, "f:\\encode.txt");

    }

}
